﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TeamManager : MonoBehaviour {

    public static TeamManager _instance;

    public int PlayerFoodNum;
    public List<Food> PlayerFoods = new List<Food>();
    public int EnemyFoodNum;
    public List<Food> EnemyFoods = new List<Food>();
    public int CurrenPlayerHealth;
    public int MaxPlayerHealth;
    public int CurrenEnemyHealth;
    public int MaxEnemyHealth;
    
    private Dictionary<int, Food> playerFoodDict = new Dictionary<int, Food>();
    private Dictionary<int, Food> EnemyFoodDict = new Dictionary<int, Food>();


    void Awake()
    {
        _instance = this;
    }

    void Start()
    {
        CreatPlayerFoodList(PlayerFoodNum);
        CreatEnemyFoodList(EnemyFoodNum);
        PlayerFoods.Sort(new MyComparer());
        EnemyFoods.Sort(new MyComparer());
        CurrenPlayerHealth = 0;
        foreach (Food food in PlayerFoods)
        {
            CurrenPlayerHealth += food.foodInfo.Health;
        }
        MaxPlayerHealth = CurrenPlayerHealth;
        CurrenEnemyHealth = 0;
        foreach (Food food in EnemyFoods)
        {
            CurrenEnemyHealth += food.foodInfo.Health;
        }
        MaxEnemyHealth = CurrenEnemyHealth;
        //Debug.Log("玩家生命" + CurrenPlayerHealth + " / " + MaxPlayerHealth + "        敌人生命" + CurrenEnemyHealth + " / " + MaxEnemyHealth);
    }

    /// <summary>
    /// 创建玩家的食物
    /// </summary>
    /// <param name="num"></param>
    public void CreatPlayerFoodList(int num)
    {
        int[] allfoodsid = ReadXml._instance.GetAllSweetFoodsID();

        int len = allfoodsid.Length;
        int[] tempGodsId = new int[len];
        System.Array.Copy(allfoodsid, tempGodsId, len);

        for (int i = 0; i < num; i++)
        {
            int index = Random.Range(0, len);
            //随机坐标
            int mapPosX = Random.Range(0, MapManager._instance.MapX);
            //int mapPosY = Random.Range(0, MapManager._instance.MapY);
            int mapPosY = 0;

            int mapNum = MapManager._instance.CalcuMapGridID(mapPosX, mapPosY);
            GameObject mapgrid = MapManager._instance.GetMapGridGo(mapNum);

            if (mapgrid.transform.childCount == 0)
            {
                Food food = new Food();
                food.Init(tempGodsId[index], mapPosX, mapPosY);
                food.foodGo.tag = "PlayerFood";
                playerFoodDict.Add(tempGodsId[index], food);
                PlayerFoods.Add(food);
                tempGodsId[index] = tempGodsId[len - 1];
                len--;
            }
            else
                i--;        
        }
    }

    /// <summary>
    /// 创建敌人的食物
    /// </summary>
    /// <param name="num"></param>
    public void CreatEnemyFoodList(int num)
    {
        int[] allfoodsid = ReadXml._instance.GetAllSaltFoodsID();

        int len = allfoodsid.Length;
        int[] tempGodsId = new int[len];
        System.Array.Copy(allfoodsid, tempGodsId, len);

        for (int i = 0; i < num; i++)
        {
            int index = Random.Range(0, len);
            //随机坐标
            int mapPosX = Random.Range(0, MapManager._instance.MapX);
            //int mapPosY = Random.Range(0, MapManager._instance.MapY);
            int mapPosY = 11;

            int mapNum = MapManager._instance.CalcuMapGridID(mapPosX, mapPosY);
            GameObject mapgrid = MapManager._instance.GetMapGridGo(mapNum);

            if (mapgrid.transform.childCount == 0)
            {
                Food food = new Food();
                //Debug.Log(tempGodsId[index]);
                food.Init(tempGodsId[index], mapPosX, mapPosY);
                food.foodGo.tag = "EnemyFood";
                food.foodView.FoodImage.color = Color.blue;
                EnemyFoodDict.Add(tempGodsId[index], food);
                EnemyFoods.Add(food);
                tempGodsId[index] = tempGodsId[len - 1];
                len--;
            }
            else
                i--;
        }
    }


    /// <summary>
    /// 根据ID来得到是哪个食物
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public Food GetPlayerFoodById(int id)
    {
        Food food = null;

        playerFoodDict.TryGetValue(id, out food);

        return food;
    }

    public Food GetEnemyFoodById(int id)
    {
        Food food = null;

        EnemyFoodDict.TryGetValue(id, out food);

        return food;
    }

    //public void DestroyFood(Food food)
    //{

    //    //Debug.Log(food.foodInfo.FoodName);
    //    if (food.foodGo == null)
    //        return;

    //    string tag = food.foodGo.tag;
    //    //Debug.Log(tag);
        
    //    if (tag == "EnemyFood")
    //    {
    //        EnemyFoods.Remove(food);

    //        EnemyFoodDict.Remove(food.foodInfo.ID);
    //    }
    //    else
    //    {
    //        PlayerFoods.Remove(food);

    //        playerFoodDict.Remove(food.foodInfo.ID);
    //    }

    //    Destroy(food.foodGo);
    //}
}

/// <summary>
/// 根据速度排序的方法，在sort里面调用
/// </summary>
public class MyComparer : IComparer<Food>
{
    public int Compare(Food x, Food y)
    {
        if (x.foodInfo.Speed > y.foodInfo.Speed)
            return -1;
        else if (x.foodInfo.Speed == y.foodInfo.Speed)
            return 0;
        else
            return 1;
    }
}
